//----------------------------------------------------------------------------
// View 'feedback;
//----------------------------------------------------------------------------
Ti.include('lib/include.js');
Ti.include('lib/constants.js');
Ti.include('lib/strings.js');
Ti.include('lib/stringbuilder.js');


var winFeedback = Titanium.UI.currentWindow;
winFeedback.addEventListener('focus', function(e) {
   log("Window 'Feedback' gekozen");
});


var viewFeedbackForm = Ti.UI.createScrollView({
   width: '100%',
   contentWidth : 'auto',
   contentHeight : 'auto',
   showVerticalScrollIndicator : true,
   showHorizontalScrollIndicator : true
});

viewFeedbackForm.add(Titanium.UI.createLabel({
   textAlign : Ti.UI.TEXTALIGNMENTLEFT,
   font : {
      fontSize : 12,
      fontWeight : 'bold',
      fontFamily : customFont
   },
   color : middenblauw,
   top : '20',
   left : '20',
   right: '20',
   text : T_FEEDBACK_FORM_HEADER
}));

var tfNaam = Ti.UI.createTextField({
   top : '75',
   width : '90%',
   height : '30',
   paddingLeft : '3',
   backgroundColor: wit,
   font : {
      fontSize : 16,
      fontWeight : 'normal'
   },
   hintText : T_FEEDBACK_FORM_NAME,
   color : middenblauw,
   borderRadius : '2',
   borderColor : middenblauw,
   borderStyle : Titanium.UI.INPUTBORDERSTYLELINE
});

var tfFeedback = Ti.UI.createTextArea({
   top : '110',
   width : '90%',
   height : '200',
   paddingLeft : '3',
   backgroundColor: wit,
   font : {
      fontSize : 16,
      fontWeight : 'normal'
   },
   hintText : T_FEEDBACK_FORM_COMMENT,
   color : middenblauw,
   borderRadius : '2',
   borderColor : middenblauw,
   borderStyle : Titanium.UI.INPUTBORDERSTYLELINE,
   horizontalWrap: true
});

//----- Buttons
var btnFeedbackFormReset = Ti.UI.createButton({
   top : '330',
   left : '20',
   width : '100',
   height : '30',
   font : {
      fontSize : 14,
      fontWeight : 'bold',
      fontFamily : customFont
   },
   color : wit,
   title : T_FEEDBACK_FORM_BTN_RESET,
   backgroundColor : hh_lichtblauw,
   backgroundImage : 'images/backgrounds/bg-lichtblauw.png',
   borderRadius : '3',
   borderColor : hh_lichtblauw
});
btnFeedbackFormReset.addEventListener('click', function(event) {
   clearFormDialog.show();
});

var btnFeedbackFormSend = Ti.UI.createButton({
   top: '330',
   right : '20',
   width : '100',
   height : '30',
   font : {
      fontSize : 14,
      fontWeight : 'bold',
      fontFamily : customFont
   },
   color : wit,
   title : T_FEEDBACK_FORM_BTN_SEND,
   backgroundColor : hh_lichtblauw,
   backgroundImage : 'images/backgrounds/bg-lichtblauw.png',
   borderRadius : '3',
   borderColor : hh_lichtblauw
});
btnFeedbackFormSend.addEventListener('click', function(event) {
   if (isValidFeedbackForm()) {
      sendFormDialog.show();
   }
});


//----- Dialogs
var clearFormDialog = Titanium.UI.createAlertDialog({
   title : T_FEEDBACK_FORM_DLG_RESET_TITLE,
   message : T_FEEDBACK_FORM_DLG_RESET_MESSAGE,
   buttonNames : [T_OK, T_CANCEL],
   cancel : 1
});
clearFormDialog.addEventListener('click', function(event) {
   if (event.index == 0) {
      clearFormTextFields();
      Ti.Analytics.featureEvent(APP_EVENTS.EVENT_HHRECEPT_FORM_CLEARED);
   }
});

var sendFormDialog = Titanium.UI.createAlertDialog({
   title : T_FEEDBACK_FORM_DLG_SEND_TITLE,
   message : T_FEEDBACK_FORM_DLG_SEND_MESSAGE,
   buttonNames : [T_OK, T_CANCEL],
   cancel : 1
});
sendFormDialog.addEventListener('click', function(event) {
   if (event.index == 0) {
      sendFeedbackForm();
   }
});


//----- UI componenten toevoegen
viewFeedbackForm.add(tfNaam, tfFeedback, btnFeedbackFormReset, btnFeedbackFormSend);
winFeedback.add(viewFeedbackForm);


//----- Functions
function clearFormTextFields() {
   log("Invoervelden wissen");
   tfNaam.value = '';
   tfFeedback.value = ''; 
}

function isValidFeedbackForm() {
   //Controleer of verplichte velden zijn ingevuld
   if (tfNaam.value == "") {
      Titanium.UI.createAlertDialog({
         title : T_FEEDBACK_FORM_DLG_CHECKS_TITLE,
         message : T_FEEDBACK_FORM_DLG_CHECKS_NAME
      }).show();
      return false;
   }
   if (tfFeedback.value == "") {
      Titanium.UI.createAlertDialog({
         title : T_FEEDBACK_FORM_DLG_CHECKS_TITLE,
         message : T_FEEDBACK_FORM_DLG_CHECKS_FEEDBACK
      }).show();
      return false;
   } 
   return true;  
}

function sendFeedbackForm() {
   var sb = new StringBuilder();
   sb.append("[Automatisch feedback formulier]\n");
   sb.append("\n");
   sb.append("Patient: ");
   sb.append(tfNaam.value);
   sb.append("\n\n");
   sb.append("Feedback:\n");
   sb.append(tfFeedback.value);
   
   var emailDialog = Ti.UI.createEmailDialog();
   emailDialog.subject = T_FEEDBACK_FORM_DLG_SEND_SUBJECT;
   emailDialog.toRecipients = [T_FEEDBACK_FORM_DLG_SEND_RECEPIENT];
   emailDialog.messageBody = sb.toString();
   emailDialog.html = false;
   
   emailDialog.open();
   emailDialog.addEventListener('complete', function(event) {
      if (event.result == emailDialog.SENT) {
         log("Bericht verzonden");
         alert('Feedback verstuurd. Wij danken u voor uw bijdrage.');
         clearFormTextFields();
         Ti.Analytics.featureEvent(APP_EVENTS.EVENT_FEEDBACK_FORM_SENT);
      }
      else if (event.result == emailDialog.CANCELLED) {
         log("Bericht versturen geannuleerd");
      }
      else {
         log("Onbekende fout opgetreden: " + event.toString);
      }
   });
}
